<template>
  <component
    is="a-modal"
    v-model:visible="visible"
    :width="600"
    :title="title"
    :mask-closable="false"
    :ok-loading="loading"
    @cancel="close"
    @before-ok="submit">
    <!-- 表单信息 start -->
    <a-form ref="formRef" :model="formData" :rules="rules" :auto-label-width="true">
      <a-row :gutter="10">
        <a-col :span="24">
          <a-form-item label="等级名称" field="name">
            <a-input v-model="formData.name" placeholder="请输入等级名称" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="等级折扣" field="discount">
            <a-input v-model="formData.discount" placeholder="请输入等级折扣" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="同时任务数量" field="task_number">
            <a-input v-model="formData.task_number" placeholder="请输入同时任务数量" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="每日任务上限" field="day_number">
            <a-input v-model="formData.day_number" placeholder="请输入每日任务上限" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="基础价格" field="price_base">
            <a-input v-model="formData.price_base" placeholder="请输入基础价格" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="日卡价格" field="price_day">
            <a-input v-model="formData.price_day" placeholder="请输入日卡价格" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="周卡价格" field="price_week">
            <a-input v-model="formData.price_week" placeholder="请输入周卡价格" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="月卡价格" field="price_mon">
            <a-input v-model="formData.price_mon" placeholder="请输入月卡价格" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="季卡价格" field="price_quarter">
            <a-input v-model="formData.price_quarter" placeholder="请输入季卡价格" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="年卡价格" field="price_year">
            <a-input v-model="formData.price_year" placeholder="请输入年卡价格" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="排序" field="sort">
            <a-input v-model="formData.sort" placeholder="请输入排序" />
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
    <!-- 表单信息 end -->
  </component>
</template>

<script setup>
import { ref, reactive, computed } from 'vue'
import { Message, Modal } from '@arco-design/web-vue'
import commonApi from '@/api/common'
import api from '../api/level'

const emit = defineEmits(['success'])
// 引用定义
const visible = ref(false)
const loading = ref(false)
const formRef = ref()
const mode = ref('')
const optionData = reactive({
})

let title = computed(() => {
  return '会员等级管理' + (mode.value == 'add' ? '-新增' : '-编辑')
})

// 表单信息
const formData = reactive({
  id: null,
  name: '',
  discount: '',
  task_number: null,
  day_number: null,
  price_base: '',
  price_day: '',
  price_week: '',
  price_mon: '',
  price_quarter: '',
  price_year: '',
  sort: null,
})

// 验证规则
const rules = {
  name: [{ required: true, message: '等级名称必需填写' }],
  discount: [{ required: true, message: '等级折扣必需填写' }],
  task_number: [{ required: true, message: '同时任务数量必需填写' }],
  day_number: [{ required: true, message: '每日任务上限必需填写' }],
  price_base: [{ required: true, message: '基础价格必需填写' }],
  price_day: [{ required: true, message: '日卡价格必需填写' }],
  price_week: [{ required: true, message: '周卡价格必需填写' }],
  price_mon: [{ required: true, message: '月卡价格必需填写' }],
  price_quarter: [{ required: true, message: '季卡价格必需填写' }],
  price_year: [{ required: true, message: '年卡价格必需填写' }],
  sort: [{ required: true, message: '排序必需填写' }],
}

// 打开弹框
const open = async (type = 'add') => {
  mode.value = type
  visible.value = true
  formRef.value.resetFields()
  await initPage()
}

// 初始化页面数据
const initPage = async () => {
}

// 设置数据
const setFormData = async (data) => {
  for (const key in formData) {
    if (data[key] != null && data[key] != undefined) {
      formData[key] = data[key]
    }
  }
}

// 数据保存
const submit = async (done) => {
  const validate = await formRef.value?.validate()
  if (!validate) {
    loading.value = true
    let data = { ...formData }
    let result = {}
    if (mode.value === 'add') {
      // 添加数据
      data.id = undefined
      result = await api.save(data)
    } else {
      // 修改数据
      result = await api.update(data.id, data)
    }
    if (result.code === 200) {
      Message.success('操作成功')
      emit('success')
      done(true)
    }
    // 防止连续点击提交
    setTimeout(() => {
      loading.value = false
    }, 500)
  }
  done(false)
}

// 关闭弹窗
const close = () => (visible.value = false)

defineExpose({ open, setFormData })
</script>
